package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tgroup;
import com.ljkj.access.Tlog;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.TGroupDAO;
import com.ljkj.common.db.util.DateTime;

public class TGroupService {
	
	private TGroupDAO tGroupDAO;
	private TLogService tLogService;

	/**
	 * 获取权限列表
	 * @param params 权限名称关键字
	 * @param currentPage 当前页
	 * @param pageSize 每页条数
	 * @param sortField 排序字段
	 * @param sortOrder 排序方式
	 * @return
	 */
	public String getGroupByProperties(String params,int currentPage,int pageSize,String sortField,String sortOrder){
		
		Class[] pojos = {Tgroup.class};
		String[] propertyNames=null;
		Object[] values=null;
		String[] likePropertyNames = null;
		Object[] likevalues= null;
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.tGroupDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.tGroupDAO.setPageMaxCount(pageSize);
		List<Tgroup> tgroups = this.tGroupDAO.findByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(Tgroup tgroup : tgroups){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("tgId",tgroup.getTgId());
			entry_map.put("parentTgId", tgroup.getParentTgId());
			entry_map.put("groupName", tgroup.getGroupName());
			entry_map.put("description", tgroup.getDescription());
			entry_map.put("createTime",tgroup.getCreateTime());
			entry_map.put("createUser", tgroup.getCreateUser());
			entry_map.put("updateTime", tgroup.getUpdateTime());
			entry_map.put("updateUser", tgroup.getUpdateUser());
			entry_map.put("disabledState", tgroup.getDisabledState());
			
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	
	
	
	/**
	 * 新增或更新用户组
	 * @param zdgCoupon
	 * @return
	 */
	public String addOrEditGroupInfo(Tgroup tgroup,Tuser tuser){
		
		Tlog tlog = new Tlog();
		
		Map<String,String> result_map = new HashMap<String, String>();
		if(tgroup.getTgId() == null){
			
			tlog.setOpType(1);
			tlog.setContent("新增用户组："+ tgroup.getGroupName());
			
			tgroup.setCreateTime(DateTime.getDateTime());
			tgroup.setCreateUser(tuser.getLoginName());
			if(this.saveNewTgroup(tgroup)){
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				result_map.put("err_msg", "新增成功。");
				result_map.put("err_code", "0");
			}else{
				result_map.put("err_msg", "新增失败。");
				result_map.put("err_code", "1");
			}
			
		}else{
			try{
				
				if(tgroup.getDisabledState()!=null && tgroup.getDisabledState() == 1){
					tlog.setOpType(0);
					tlog.setContent("用户组状态："+tgroup.getGroupName()+" 变为不可用");
				}else{
					tlog.setOpType(1);
					tlog.setContent("修改用户组："+tgroup.getGroupName());
				}
				
				tgroup.setUpdateTime(DateTime.getDateTime());
				tgroup.setUpdateUser(tuser.getLoginName());
				this.tGroupDAO.update(tgroup);
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				
				result_map.put("err_msg", "更新成功。");
				result_map.put("err_code", "0");
			}catch(Exception ex){
				result_map.put("err_msg", "更新失败。");
				result_map.put("err_code", "1");
			}
		}
		
		return JSON.toJSONString(result_map);
	}
	
	public boolean saveNewTgroup(Tgroup tgroup){
		try{
			this.tGroupDAO.save(tgroup);
			return true;
		}catch(Exception ex){
			return false;
		}
	}
	
	public Tgroup getTgroupById(Integer id){
		return this.tGroupDAO.findById(id);
	}

	
	
	//注入DAO
	public void settGroupDAO(TGroupDAO tGroupDAO) {
		this.tGroupDAO = tGroupDAO;
	}
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}

}
